//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.滑动窗口.定长滑动窗口;

public class demo_1052 {
    public demo_1052() {
    }

    public static void main(String[] args) {
        int[] cust = new int[]{7, 8, 8, 6};
        int[] grumpy = new int[]{0, 1, 0, 1};
        System.out.println(maxSatisfied(cust, grumpy, 3));
    }

    public static int maxSatisfied(int[] customers, int[] grumpy, int minutes) {
        int cN = customers.length;
        int gN = grumpy.length;
        int max = 0;
        int l = 0;
        int r = 0;
        int sum = 0;
        int count = 0;

        for(int i = 0; i < cN; ++i) {
            if (grumpy[i] == 0) {
                count += customers[i];
            }

            if (grumpy[i] == 1) {
                sum += customers[i];
            }

            if (i >= minutes - 1) {
                int out = i - minutes + 1;
                if (sum > max) {
                    max = sum;
                    l = out;
                    r = i;
                }

                if (grumpy[out] == 1) {
                    sum -= customers[i - minutes + 1];
                }
            }
        }

        for(int i = l; i <= r; ++i) {
            if (grumpy[i] == 1) {
                count += customers[i];
            }
        }

        return count;
    }

    public static int maxSatisfied2(int[] customers, int[] grumpy, int minutes) {
        int max = 0;
        int sum = 0;
        int count = 0;

        for(int i = 0; i < customers.length; ++i) {
            if (grumpy[i] == 0) {
                count += customers[i];
            }

            if (grumpy[i] == 1) {
                sum += customers[i];
            }

            if (i >= minutes - 1) {
                max = Math.max(max, sum);
                int out = i - minutes + 1;
                if (grumpy[out] == 1) {
                    sum -= customers[i - minutes + 1];
                }
            }
        }

        return count + max;
    }
}
